﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RaytracerLib.MathLib;
using YAXLib;

namespace RaytracerLib.Attenuations
{
    public class ExponentalAttenuation : Attenuation
    {
        private ColorFloat m_real_alpha;

        [YAXNode]
        public ColorFloat Alpha;

        public ExponentalAttenuation() 
        {
        }

        public ExponentalAttenuation(ColorFloat a_alpha)
        {
            Alpha = a_alpha;
        }

        public ExponentalAttenuation(double a_r, double a_g, double a_b)
        {
            Alpha = new ColorFloat(a_r, a_g, a_b);
        }

        public override void CalculateAttenuation(double a_light_speed)
        {
            m_real_alpha = Alpha / a_light_speed;
        }

        public override ColorFloat Attenuate(ColorFloat a_light, double a_dist)
        {
            return new ColorFloat(
                a_light.R * Math.Exp(-a_dist * m_real_alpha.R),
                a_light.G * Math.Exp(-a_dist * m_real_alpha.G),
                a_light.B * Math.Exp(-a_dist * m_real_alpha.B));
        }

        public override bool Equals(object a_obj)
        {
            if (Object.ReferenceEquals(a_obj, this))
                return true;

            ExponentalAttenuation att = a_obj as ExponentalAttenuation;
            if (att == null)
                return false;

            return Alpha.IsAlmostRelativeEquals(att.Alpha);
        }

        public override int GetHashCode()
        {
            return Alpha.GetHashCode();
        }
    }
}
